﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using Osobe;
using SkolskaUstanova;
using MolbeIPotvrde;

namespace ElektronskiDnevnik
{
     public partial class IUcenik : Form
     {

        MySqlDataReader rdr = null;
        MySqlConnection conn = new MySqlConnection("SERVER='etf.ba';" + "DATABASE='ooadtim8';" + "UID='ooadtim8';" + "PASSWORD='ooadtim8';");
        Prijavljivanje stara = new Prijavljivanje();
        Ucenik ucenik;
        List<int> ocjene;
        public IUcenik(Prijavljivanje f, String maticni)
        {
            InitializeComponent();
            stara = f;
            ocjene = new List<int>();
            MySqlCommand cmd = new MySqlCommand("SELECT * FROM Ucenik WHERE JMBG = '" + maticni + "';", conn);
            try
            {
                conn.Open();
                rdr = cmd.ExecuteReader();
                while (rdr.Read())
                {
                    ucenik = new Ucenik(Convert.ToString(rdr["Ime"]), Convert.ToString(rdr["Prezime"]), Convert.ToString(rdr["JMBG"]), Convert.ToDateTime(rdr["DatumRodjenja"]), Convert.ToString(rdr["MjestoRodjenja"]), Convert.ToString(rdr["Ulica"]) + ' ' + Convert.ToString(rdr["Broj"]), Convert.ToString(rdr["Password"]), Convert.ToInt32(rdr["Razred"]));
                    pictureBox1.ImageLocation = Convert.ToString(rdr["URLSlike"]);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Greska", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                if (rdr != null)
                    rdr.Close();
            }
        }

        private void Form2_Paint(object sender, PaintEventArgs e)
        {
            Font font = new Font("Segoe Print", 28, FontStyle.Regular);
            SolidBrush brush = new SolidBrush(Color.Salmon);
            StringFormat sf = new StringFormat();
            sf.Alignment = StringAlignment.Center;
            e.Graphics.DrawString("Dobrodošao/la, " + ucenik.Ime, font, brush,200, 30);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            PregledUspjehaU nova = new PregledUspjehaU(ucenik.Jmbg, conn);
            nova.Show();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            PregledPrisustvaU nova = new PregledPrisustvaU(ucenik.Jmbg, conn);
            nova.Show();
     
        }

        private void button3_Click(object sender, EventArgs e)
        {
            PromjenaPassworda nova = new PromjenaPassworda(ucenik.Jmbg, conn, "1");
            nova.Show();
        }


        private void button5_Click(object sender, EventArgs e)
        {
            var rez = MessageBox.Show("Sigurni ste da se zelite odjaviti?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (rez == DialogResult.No) return;
            else
            {
                if (conn != null)
                    conn.Close();
                this.Close();
                stara.Show();
            }
        }

        private void Form2_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (conn != null)
                conn.Close();
            stara.Show();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            textBox1.Text = ucenik.Ime;
            textBox10.Text = ucenik.Prezime;
            textBox2.Text = ucenik.Jmbg;
            textBox3.Text = Convert.ToString((ucenik.DatumRodjenja).Day + "/" + (ucenik.DatumRodjenja).Month + "/" + (ucenik.DatumRodjenja).Year);
            textBox4.Text = ucenik.MjestoRodjenja;
            textBox5.Text = ucenik.Adresa;
            MySqlDataReader reader = null;
            MySqlCommand cmd1 = new MySqlCommand("SELECT * FROM Razred WHERE id = " + ucenik.Razred + ";",conn);
            try
            {
                reader = cmd1.ExecuteReader();
                while (reader.Read())
                {
                    textBox6.Text = Convert.ToString(reader["Godina"]) + '-' + Convert.ToString(reader["Odjeljenje"]);

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Greska", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                if (reader != null)
                    reader.Close();
            }
            MySqlCommand cmd = new MySqlCommand("SELECT * FROM ProsjekPoPredmetu WHERE JMBG='" + ucenik.Jmbg + "';", conn);
            int suma = 0;
            try
            {
                reader = cmd.ExecuteReader();
                while (reader.Read())
                    ocjene.Add(Convert.ToInt32(reader["Prosjek"]));
                for (int i = 0; i < ocjene.Count; i++)
                {
                    if (ocjene[i] == 1)
                    {
                        suma = 1;
                        break;
                    }
                    else
                        suma += ocjene[i];
                }
                double prosjek;
                if (suma != 1)
                    prosjek = Convert.ToDouble(suma)/ocjene.Count();
                else
                    prosjek = 1;
                textBox7.Text = Convert.ToString(prosjek);

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Greska", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                if (reader != null)
                    reader.Close();
            }
        }


        }
}
